var my_media = null;
function startRecord() {
    function onSuccess(successInfo) {
        alert(successInfo);
    }

    function onError(errorInfo) {
        alert("MediaErrorInfo:"+errorInfo);
    }

    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dirEntry) {
        var targetPath = dirEntry.toURL() + "test.m4a";
        my_media = new Media(targetPath, onSuccess, onError);
        /*
        var counter = 0;
        var timerDur = setInterval(function() {
            counter = counter + 100;
            if (counter > 2000) {
                clearInterval(timerDur);
            }
            var dur = my_media.getDuration();
            if (dur > 0) {
                clearInterval(timerDur);
                document.getElementById('mediaInfo').innerHTML = (dur) + " sec";
            }

        }, 1000);*/
        /*
        var mediaTimer = setInterval(function () {
            // get media position
            my_media.getCurrentPosition(
                // success callback
                function (position) {
                    if (position > -1) {
                        console.log((position) + " sec");
                    }
                },
                // error callback
                function (e) {
                    console.log("Error getting pos=" + e);
                }
            );
        }, 1000);
        */
        my_media.startRecord();
    });

    document.getElementById("mediaInfo").innerHTML = "正在录音";
}

function pauseRecord() {
    my_media.pauseRecord();
    document.getElementById("mediaInfo").innerHTML = "录音已暂停";
}

function resumeRecord() {
    my_media.resumeRecord();
    document.getElementById("mediaInfo").innerHTML = "正在录音";
}

function stopRecord() {
    my_media.stopRecord();
    document.getElementById("mediaInfo").innerHTML = "录音结束";
}

function startPlaying() {
    if(my_media != null) {
        my_media.play();
        document.getElementById("mediaInfo").innerHTML = "正在播放";
    }
}

function pausePlaying() {
    if(my_media != null) {
        my_media.pause();
        document.getElementById("mediaInfo").innerHTML = "已暂停播放";
    }
}

function resumePlaying() {
    if(my_media != null) {
        my_media.play();
        document.getElementById("mediaInfo").innerHTML = "正在播放";
    }
}

function stopPlaying() {
    if(my_media != null) {
        my_media.stop();
        document.getElementById("mediaInfo").innerHTML = "已停止播放";
    }
}

function setVolume0() {
    if(my_media != null) {
        my_media.setVolume(0);
        document.getElementById("mediaInfo").innerHTML = "关闭声音";
    }
}

function setVolume1() {
    if(my_media != null) {
        my_media.setVolume(1);
        document.getElementById("mediaInfo").innerHTML = "原始声音";
    }
}

function seekTo() {
    if(my_media != null) {
        my_media.seekTo(1000);
        document.getElementById("mediaInfo").innerHTML = "设置播放进度";
    }
}

function setRate() {
    if(my_media != null) {
        my_media.setRate(0.5);
        document.getElementById("mediaInfo").innerHTML = "设置慢速播放";
    }
}

function releaseAudio() {
    if(my_media != null) {
        my_media.release();
        document.getElementById("mediaInfo").innerHTML = "已释放音频";
        my_media = null;
    }
}
